from django.core.management.base import BaseCommand
from goods.models import ProductCategory, Product
from decimal import Decimal

class Command(BaseCommand):
    help = '创建商品测试数据'

    def handle(self, *args, **options):
        self.stdout.write('开始创建测试数据...')
        
        # 创建商品分类
        categories = {
            '电子产品': {
                '手机': [
                    {'name': 'iPhone 15', 'code': 'IP15', 'price': 5999.00, 'description': '苹果最新手机，搭载A17 Pro芯片'},
                    {'name': '华为 Mate 60', 'code': 'HW60', 'price': 6999.00, 'description': '华为旗舰手机，支持卫星通信'},
                    {'name': '小米 14', 'code': 'XM14', 'price': 3999.00, 'description': '小米年度旗舰，徕卡光学'},
                ],
                '电脑': [
                    {'name': 'MacBook Pro 14', 'code': 'MBP14', 'price': 14999.00, 'description': '苹果专业级笔记本电脑'},
                    {'name': 'ThinkPad X1', 'code': 'TPX1', 'price': 12999.00, 'description': '联想商务笔记本'},
                    {'name': 'Dell XPS 13', 'code': 'DX13', 'price': 8999.00, 'description': '戴尔超薄笔记本'},
                ]
            },
            '服装': {
                '男装': [
                    {'name': '休闲T恤', 'code': 'T001', 'price': 99.00, 'description': '纯棉舒适休闲T恤'},
                    {'name': '牛仔裤', 'code': 'J001', 'price': 199.00, 'description': '经典蓝色牛仔裤'},
                ],
                '女装': [
                    {'name': '连衣裙', 'code': 'D001', 'price': 299.00, 'description': '优雅夏季连衣裙'},
                    {'name': '针织衫', 'code': 'K001', 'price': 159.00, 'description': '柔软针织毛衣'},
                ]
            },
            '家居': {
                '家具': [
                    {'name': '实木餐桌', 'code': 'T001', 'price': 1299.00, 'description': '北欧风格实木餐桌'},
                    {'name': '布艺沙发', 'code': 'S001', 'price': 2499.00, 'description': '舒适三人布艺沙发'},
                ],
                '装饰': [
                    {'name': '花瓶套装', 'code': 'V001', 'price': 89.00, 'description': '陶瓷花瓶装饰套装'},
                    {'name': '壁画', 'code': 'W001', 'price': 199.00, 'description': '现代简约壁画'},
                ]
            }
        }
        
        created_categories = {}
        created_products = 0
        
        for main_cat_name, sub_cats in categories.items():
            # 创建主分类
            main_category, created = ProductCategory.objects.get_or_create(
                name=main_cat_name,
                defaults={'parent': None}
            )
            if created:
                self.stdout.write(f'创建主分类: {main_cat_name}')
            created_categories[main_cat_name] = main_category
            
            for sub_cat_name, products in sub_cats.items():
                # 创建子分类
                sub_category, created = ProductCategory.objects.get_or_create(
                    name=sub_cat_name,
                    defaults={'parent': main_category}
                )
                if created:
                    self.stdout.write(f'创建子分类: {sub_cat_name}')
                created_categories[sub_cat_name] = sub_category
                
                # 创建商品
                for product_data in products:
                    product, created = Product.objects.get_or_create(
                        code=product_data['code'],
                        defaults={
                            'name': product_data['name'],
                            'category': sub_category,
                            'description': product_data['description'],
                            'price': Decimal(str(product_data['price'])),
                            'is_active': True
                        }
                    )
                    if created:
                        created_products += 1
                        self.stdout.write(f'创建商品: {product_data["name"]}')
        
        self.stdout.write(
            self.style.SUCCESS(
                f'测试数据创建完成！共创建 {len(created_categories)} 个分类，{created_products} 个商品'
            )
        ) 